package cn.yzx.pethome.system.sdt.controller;

import cn.yzx.pethome.system.sdt.domain.SystemDictionaryDetail;
import cn.yzx.pethome.system.sdt.query.SystemDictionaryDetailQuery;
import cn.yzx.pethome.system.sdt.service.ISystemDictionaryDetailService;
import cn.yzx.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sdd")
@Api(value = "数据字典明细的API",description="数据字典明细相关的CRUD功能")
public class SystemDictionaryDetailController {
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    @PostMapping("")
    @ApiOperation(value = "通过数据字典Id分页查询" )
    public AjaxResult findByIdPage(@RequestBody SystemDictionaryDetailQuery query){
        try {
            List<SystemDictionaryDetail> datas = systemDictionaryDetailService.findByTypeId(query);
            Long count = systemDictionaryDetailService.findByQueryCount(query);
            return  AjaxResult.success().setData(datas).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除" )
    public AjaxResult deleteById(@PathVariable Long id){
        try {
            systemDictionaryDetailService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    @PutMapping
    @ApiOperation(value = "保存字典明细信息【有id为修改】" )
    public AjaxResult save(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try{
            if (systemDictionaryDetail.getId() != null){
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }else {
                systemDictionaryDetailService.add(systemDictionaryDetail);
            }
            return AjaxResult.success().setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("保存失败");
    }
}
